package regexp;

import java.util.regex.Pattern;

/**
 * @author DCSGO
 * @version 1.0
 * Pattern.matches 整体匹配
 * 判断正则表达式匹配的结果是否是整个字符串
 */
public class PatternMethod {
    public static void main(String[] args) {
        //是否为纯数字
        System.out.println(Pattern.matches("\\d+", "123"));//true
        System.out.println(Pattern.matches("\\d+", "1 23"));//false
        System.out.println(Pattern.matches("\\d+", "a"));//false

        String content2 = "https://fanyi.baidu.com/#en/zh/matches";
        String content3 = "1https://fanyi.baidu.com/#en/zh/matches";

        String regStr = "((http|https)://)?([\\w-]+\\.)+[\\w-]+(/[\\w-&%#=?./]*)?";
        boolean matches2 = Pattern.matches(regStr, content2);
        boolean matches3 = Pattern.matches(regStr, content3);
        System.out.println("整体匹配的结果是：" + matches2);//整体匹配的结果是：true
        System.out.println("整体匹配的结果是：" + matches3);//整体匹配的结果是：false

        /* 底层调用的还是 Matcher 中的 matches 方法
         * public static boolean matches(String regex, CharSequence input) {
         *         Pattern p = Pattern.compile(regex);
         *         Matcher m = p.matcher(input);
         *         return m.matches();
         *     }
         */
    }
}
